package com.teach.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.teach.commons.JDBCUtil;
import com.teach.entity.Config;
import com.teach.entity.Lj;

public class ConfigDAO {


	/**
	 *  - 添加一个系统配置
	 *  - 包含所有字段
	 * @param config - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int insert(Config config) throws SQLException{
		String sql = "insert into config (CONFIG_KEY,CONFIG_VALUE) value(?,?)";
		try(
				Connection con = JDBCUtil.getConnection();
				PreparedStatement pstmt = con.prepareStatement(sql);
				){
			pstmt.setObject(1, config.getConfigKey());
			pstmt.setObject(2, config.getConfigValue());
			return pstmt.executeUpdate();
		}
		
	}
	
	/**
	 *  - 根据主键删除一个系统配置
	 * @param configKey - 主键
	 * @return
	 * @throws SQLException
	 */
	public int deleteByKey(String configkey) throws SQLException{
		String sql = "delete from config where CONFIG_KEY = ?";
		try(
				Connection con = JDBCUtil.getConnection();
				PreparedStatement pstmt = con.prepareStatement(sql);
				){
			pstmt.setObject(1, configkey);
			return pstmt.executeUpdate();
		}
	}
	
	/**
	 *  -根据主键更新一个系统配置
	 *  -只包含非null的字段
	 * @param config - 实体对象
	 * @return - 影响行数
	 * @throws SQLException
	 */
	public int updateByKey(Config config) throws SQLException{
		String sql = "update config set CONFIG_VALUE = ? where CONFIG_KEY = ?";
		try(
				Connection con = JDBCUtil.getConnection();
				PreparedStatement pstmt = con.prepareStatement(sql);
				){
			pstmt.setObject(1, config.getConfigValue());
			pstmt.setObject(2, config.getConfigKey());
			return pstmt.executeUpdate();
		}
	}
	
	/**
	 *  -根据主键获取一个系统配置
	 * @param configKey - 主键
	 * @return
	 * @throws SQLException 没有获取到，或者获取多个都会抛出异常
	 */
	public Config selectByKey(String configkey) throws SQLException{
		String sql = "select * from config where CONFIG_KEY = ?";
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = JDBCUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			pstmt.setObject(1, configkey);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				return load(rs);
			}else {
				throw new SQLException("没有对应记录！");
			}
		}finally {
			JDBCUtil.close(con,pstmt,rs);
		}
	}
	
	public Config load(ResultSet rs) throws SQLException{
		Config config = new Config();
		config.setConfigKey(rs.getString("CONFIG_KEY"));
		config.setConfigValue(rs.getString("CONFIG_VALUE"));
		return config;
	}
	
	/**
	 *  -统计
	 * @param configKey - 实体对象
	 * @return
	 * @throws SQLException
	 */
	public long count() throws SQLException{
		String sql = "select count(*) as c from config ";
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = JDBCUtil.getConnection();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				return rs.getLong(1);
			}
		}finally {
			JDBCUtil.close(con, pstmt, rs);
		}
		return 0;
	}
}
